// pages/order/order.js

const init = require('../../utils/init')
const api = require('../../utils/api')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'',
    orderStatusName:'',
    orderStatus:"",
    pwdFlag:false,
    disabledFlag:false,
    card: [{
      title: {
        text: "订单支付",
        size:38,
        color:"#000"
      }},
      {
        title: {
          text: "查看密码",
          size:38,
          color:"#000"
        }},
        {
          title: {
            text: "取消订单",
            size:38,
            color:"#000"
          }},
          {
            title: {
              text: "箱柜位置",
              size:38,
              color:"#000"
            }}
    ],
    recordlist:[{
      name:"用柜开始时间", 
      context:'2021-21-11 10:12:12',
    },{
      name:"退柜时间",
      context:"--",
    }],

    timelist:[{
      name:"超时",
      context:"7",
      unit:"小时"
    },{
      name:"产生超时费用",
      context:"21",
      unit:"元"
    }],

    orderlist:[{
      name:"使用人",
      context:"Jevon",
    },{
      name:"手机号",
      context:"18812167612",
    },{
      name:"规格",
      context:"VIP储物柜",
    },{
      name:"租用时长",
      context:"2",
      unit:"小时"
    },{
      name:"租金",
      context:"3.2",
      unit:"元"
    },{
      name:"押金",
      context:"3.2",
      unit:"元"
    },{
      name:"下单时间",
      context:"2021-01-11 21:11:23",
    },{
      name:"订单编号",
      context:"C00012334"
    }]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      orderId:options.orderInfoId
    })
    this.getOneOrderInfo(options.orderStatus,options.orderInfoId)
  },

  onReady: function () {

  },

  onShow: function () {
    
  },

  getOneOrderInfo(status,id){
    if(id){
      api.getOneOrderInfo({id:id}).then(res=>{
        console.log(res)
        if(res.code===1){
          this.setData({
            orderStatusName:res.info.orderStatusName,
            orderStatus:res.info.orderStatus,
            orderlist:res.info.order,
            recordlist:res.info.record,
            timelist:res.info.time,
            areaInfo:res.info.areaInfo
          })
        }
      })
    }
  },

  pwdShow(){
    this.setData({
      pwdFlag:!this.data.pwdFlag
    })
  },
  refundOrder(e){
    var that = this
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '取消订单',
      content: '租赁商品30分钟内未使用，可自动取消，确定取消？',
      cancelText:'否',
      confirmText:'是',
      success (res) {
        if (res.confirm) {
          that.setData({
            disabledFlag:true
          })
          wx.showLoading({
            title: '取消订单中…',
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 2000) 

          api.wxRefund({
            orderId:id
          }).then(res=>{
            if(res.code===1){
              console.log(res)
              wx.switchTab({
                url: '/pages/item/item',
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

    
  },
  againPay(e){
    var id = e.currentTarget.dataset.id;
    api.wxAgainPay({
      orderId:id
    }).then(res=>{
      if(res.code===1){
        var json = res.info
        console.log(json)
        wx.requestPayment({
          ...json,
          success(res){
            console.log(res)
            wx.showLoading({
              title: '订单支付中…',
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 2000)            
            var timer = setInterval(function () {
              if (app.globalData.openid) {
                clearInterval(timer);
                api.getOrderInfo({
                  openId:app.globalData.openid,
                  orderStatus:['pay']
                }).then(res=>{
                  if(res.code===1){
                    console.log(res)
                    wx.switchTab({
                      url: '/pages/item/item',
                    })
                  }
                })
              }
            }, 1500)
          },fail(res){
            console.log(res)
          }
        })
     
      }

    })
  }
})